package com.ruoyi.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.DraftArticleMapper;
import com.ruoyi.mapper.ArticleMapper;
import com.ruoyi.mapper.ConlumnMapper;
import com.ruoyi.domain.DraftArticle;
import com.ruoyi.domain.Article;
import com.ruoyi.domain.Conlumn;
import com.ruoyi.service.IDraftArticleService;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;

/**
 * 草稿文章Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-16
 */
@Service
public class DraftArticleServiceImpl extends ServiceImpl<DraftArticleMapper,DraftArticle> implements IDraftArticleService
{
    @Autowired
    private DraftArticleMapper draftArticleMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ConlumnMapper conlumnMapper;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private PdfParseTaskServiceImpl pdfParseTaskService;

    
    /**
     * 查询草稿文章
     * 
     * @param articleId 草稿文章ID
     * @return 草稿文章
     */
    @Override
    public DraftArticle selectDraftArticleByArticleId(Long articleId)
    {
        return draftArticleMapper.selectDraftArticleByArticleId(articleId);
    }

    /**
     * 查询草稿文章列表
     * 
     * @param draftArticle 草稿文章
     * @return 草稿文章
     */
    @Override
    public List<DraftArticle> selectDraftArticleList(DraftArticle draftArticle)
    {
        return draftArticleMapper.selectDraftArticleList(draftArticle);
    }

    /**
     * 新增草稿文章
     *
     * @param draftArticle 草稿文章
     * @return 结果
     */
    @Override
    public int insertDraftArticle(DraftArticle draftArticle)
    {
        return draftArticleMapper.insertDraftArticle(draftArticle);
    }

    /**
     * 修改草稿文章
     *
     * @param draftArticle 草稿文章
     * @return 结果
     */
    @Override
    public int updateDraftArticle(DraftArticle draftArticle)
    {
        return draftArticleMapper.updateDraftArticle(draftArticle);
    }

    /**
     * 批量删除草稿文章
     * 
     * @param articleIds 需要删除的草稿文章ID
     * @return 结果
     */
    @Override
    public int deleteDraftArticleByArticleIds(Long[] articleIds)
    {
        return draftArticleMapper.deleteDraftArticleByArticleIds(articleIds);
    }

    /**
     * 删除草稿文章信息
     * 
     * @param articleId 草稿文章ID
     * @return 结果
     */
    @Override
    public int deleteDraftArticleByArticleId(Long articleId)
    {
        return draftArticleMapper.deleteDraftArticleByArticleId(articleId);
    }

    /**
     * 检查是否为媒体聚焦栏目
     *
     * @param columnId 栏目ID
     * @return 是否为媒体聚焦栏目
     */
    private boolean isMediaFocusColumn(Long columnId) {
        if (columnId == null) {
            return false;
        }

        // 查询栏目信息
        Conlumn column = conlumnMapper.selectConlumnById(columnId);
        if (column == null) {
            return false;
        }

        // 判断栏目名称是否为"媒体聚焦"
        return "媒体聚焦".equals(column.getName());
    }

    /**
     * 发布草稿文章
     *
     * @param articleId 草稿文章ID
     * @return 结果
     */
    @Override
    public int publishDraftArticle(Long articleId)
    {
        // 先将草稿文章插入到正式文章表中
        DraftArticle draftArticle = draftArticleMapper.selectDraftArticleByArticleId(articleId);
        if (draftArticle == null) {
            return 0; // 草稿不存在
        }

        // 检查是否上传了审批表
        if (draftArticle.getAppval() == null || draftArticle.getAppval().isEmpty()) {
            throw new RuntimeException("请上传审批表");
        }

        // 检查媒体聚焦栏目是否上传了封面图片
        if (draftArticle.getColumnId() != null && isMediaFocusColumn(draftArticle.getColumnId())) {
            // 检查封面图片是否上传
            if (draftArticle.getCoverPath() == null || draftArticle.getCoverPath().isEmpty()) {
                throw new RuntimeException("媒体聚焦栏目必须上传封面图片");
            }
        }

        // 将DraftArticle转换为Article
        Article article = new Article();
        article.setTitle(draftArticle.getTitle());
        article.setArticleOrigin(draftArticle.getArticleOrigin());
        article.setColumnId(draftArticle.getColumnId());
        article.setContent(draftArticle.getContent());
        article.setAuthor(draftArticle.getAuthor());
        article.setCoverPath(draftArticle.getCoverPath());
        article.setIp(draftArticle.getIp());
        // 设置发布日期为当前日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        article.setPubdate(sdf.format(new Date()));
        article.setAppval(draftArticle.getAppval());
        article.setAuditId(draftArticle.getAuditId());
        article.setLittleTitle1(draftArticle.getLittleTitle1());
        article.setLittleTitle2(draftArticle.getLittleTitle2());
        article.setStatus(draftArticle.getStatus());
        article.setCreateTime(draftArticle.getCreateTime());
        article.setCreateBy(draftArticle.getCreateBy());
        article.setUpdateTime(draftArticle.getUpdateTime());
        article.setUpdateBy(draftArticle.getUpdateBy());
        article.setRemark(draftArticle.getRemark());
        article.setAttachment(draftArticle.getAttachment());

        // 插入到正式文章表中
        int result = articleMapper.insertArticle(article);

        // 如果草稿有PDF附件，异步解析PDF为图片
        if (result > 0 && draftArticle.getAttachment() != null && !draftArticle.getAttachment().isEmpty()) {
            // 检查附件是否为PDF文件
            if (draftArticle.getAttachment().toLowerCase().endsWith(".pdf")) {
                // 异步处理PDF解析任务
                pdfParseTaskService.processPdfParseTask(article.getArticleId());
            }
        }

        if (result > 0) {
            // 删除草稿文章
            draftArticleMapper.deleteDraftArticleByArticleId(articleId);
        }
        return result;
    }
}